package com.lanxin.stu.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.lanxin.result.ResponseResult;
import com.lanxin.stu.dto.StudentAddDTO;
import com.lanxin.stu.dto.StudentDTO;
import com.lanxin.stu.dto.StudentSearchDTO;
import com.lanxin.stu.entity.Classes;
import com.lanxin.stu.entity.Student;
import com.lanxin.stu.mapstruct.ClassesMapStruct;
import com.lanxin.stu.mapstruct.StudentMapStruct;
import com.lanxin.stu.service.IClassesService;
import com.lanxin.stu.service.IStudentService;
import com.lanxin.stu.vo.ClassesVO;
import com.lanxin.stu.vo.StudentVO;
import com.lanxin.vo.PageVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyh
 * @since 2025-09-17
 */
@RestController
@RequestMapping("/student")
@Api(tags = "学生管理")
@Validated
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @Autowired
    private StudentMapStruct studentMapStruct;

    @ApiOperation(value = "分页查询学生信息")
    @PostMapping(value="/selectStudentPage")
    public ResponseResult<PageVO<StudentVO>> selectStudentPage(@RequestBody @Valid StudentSearchDTO studentSearchDTO){
        return ResponseResult.success(studentService.selectStudentPage(studentSearchDTO));

    }

    @ApiOperation(value="添加学生")
    @PostMapping(value="/addStudent")
    public ResponseResult addStudent(@RequestBody @Validated StudentAddDTO studentAddDTO){
        boolean flag = studentService.addStudent(studentAddDTO);
        if ( flag){
            return ResponseResult.success("添加成功");
        }
        return ResponseResult.fail("添加失败");
    }

    @ApiOperation(value="根据ID查询学生信息")
    @GetMapping(value="/selectStudentById/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生编号", required = true)
    })
    public ResponseResult<StudentVO> selectStudentById(
            @NotNull(message = "学生编号不能为空")
            @Positive(message = "学生编号不正确")
            @PathVariable("id") Integer id){
        Student student = studentService.getById(id);
        StudentVO studentVO = studentMapStruct.toVO(student);
        return ResponseResult.success(studentVO);
    }

    @ApiOperation(value="修改学生信息")
    @PutMapping(value="/updateStudent")
    public ResponseResult updateStudent(@RequestBody @Validated StudentDTO studentDTO){
        boolean flag = studentService.updateById(studentMapStruct.toEntity(studentDTO));
        if (flag){
            return ResponseResult.success("修改成功");
        }
        return ResponseResult.fail("修改失败");
    }

    @ApiOperation(value="删除学生")
    @DeleteMapping(value="/deleteStudent/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生编号", required = true)
    })
    public ResponseResult deleteStudent(
            @NotNull(message = "学生编号不能为空")
            @Positive(message = "学生编号不正确")
            @PathVariable("id") Integer id){
        boolean flag = studentService.removeById(id);
        if (flag){
            return ResponseResult.success("删除成功");
        }
        return ResponseResult.fail("删除失败");
    }
}
